import listCard from '@/components/acessory/listCard.vue' //列表卡片
import { dateFormat, dayDiff } from '@/utils/formatDate.js'

export default {
  components: {
    listCard,
  },

  data() {
    return {
      id: '',
      info: null,
      recommendList: [],
      remainder: '', //剩余时间
      quoteRemainder: '', //报价剩余时间
      showRecommend: false,
      pageFrom: '', //从哪里进入详情
      pageLoading: false, //初始化加载效果
    }
  },

  computed: {
    statusIcon() {
      return this.$store.state.accessory.statusIcon
    },
    status() {
      return this.$store.state.accessory.status
    },
    quality() {
      return this.$store.state.accessory.quality
    },
    formatName() {
      if (this.info.trueName) {
        return (
          this.info.trueName.slice(0, 1) +
          '*'.repeat(this.info.trueName.length - 1)
        )
      }
      return ''
    },
    formatPhone() {
      if (this.info.store_Phone) {
        return this.info.store_Phone.replace(/(.{3}).*(.{4})/, '$1****$2')
      }
      return ''
    },
  },

  onLoad(e) {
    this.id = e.id
    this.pageFrom = e.pageFrom ? e.pageFrom : ''
    this.getRecommendList()
  },

  onShow() {
    this.getInfo(this.id)
  },

  methods: {
    dateFormat,
    dayDiff,

    //获取订单信息
    getInfo(id) {
      if (this.pageLoading) {
        uni.showLoading()
      }
      this.$api.accessory
        .getCarPartOfferByCarPartId({
          id: id,
        })
        .then((res) => {
          this.info = res.data
          this.remainder = this.dayDiff(this.info.effective_Time)
          this.quoteRemainder = this.dayDiff(
            this.info.carPartOffer.effective_time
          )
          if (this.info.carPartOffer) {
            //有效期时间处理
            this.freightPrice = this.info.carPartOffer.freight_price
            this.remarks = this.info.carPartOffer.remarks
          }
          this.info.carPartDetails = res.data.carPartDetails.filter((item) => {
            if (item.offer_price != 0) {
              return item
            }
          })
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
        .catch((e) => {
          console.log(e)
          if (this.pageLoading) {
            uni.hideLoading()
          } else {
            this.pageLoading = true
          }
        })
    },

    //获取推荐询价
    getRecommendList() {
      this.$api.accessory
        .getRecommendList({
          pageIndex: 1,
          pageSize: 8,
          lat: this.$store.state.latitude
            ? this.$store.state.latitude
            : 104.063364,
          lon: this.$store.state.longitude
            ? this.$store.state.longitude
            : 30.566,
        })
        .then((res) => {
          console.log(res)
          if (res.data.list) {
            this.recommendList = res.data.list
            this.showRecommend = true
          }
        })
        .catch((e) => {})
    },

    //图片处理
    handdleImg(imgStr) {
      if (imgStr) {
        return JSON.parse(imgStr)
      }
    },
    //编辑
    edit() {
      uni.navigateTo({
        url:
          '/pages_findAccessory/submitQuote/submitQuote?id=' +
          this.id +
          '&type=1',
      })
    },

    //联系
    chart() {
      if (this.pageFrom === 'chat') {
        uni.navigateBack()
        return
      }
      let carPartDetailsStr = ''
      for (let i = 0; i < this.info.carPartDetails.length; i++) {
        if (i > 4) {
          break
        }
        carPartDetailsStr =
          carPartDetailsStr +
          this.info.carPartDetails[i].part_Category_Name +
          ' '
      }
      this.$store.commit('news/createConversationActive', {
        user: this.info.store_Name, //店铺名称
        img: this.info.store_topimg, //店铺头像
        lastTime: '', //
        userId: this.info.store_Id, //店铺id
        conversationID: 'C2C' + this.info.store_Id,
      })
      this.$store.commit('news/setCardInfo', {
        imgSrc:
          'https://files.yzsheng.com/goodimg/brandimg/' +
          this.info.brand_Id +
          '.png',
        name:
          this.info.brand_Name +
          ' ' +
          this.info.car_Type_Name +
          ' ' +
          this.info.configuration_Name,
        carPartDetailsStr: carPartDetailsStr,
        type: 'accessory',
      })
      uni.navigateTo({
        url:
          '/pages_news/contactSeller/contactSeller?user=' +
          this.info.store_Name +
          '&pageId=' +
          this.info.id +
          '&tab=1',
      })
    },

    //电话
    call() {
      uni.makePhoneCall({
        phoneNumber: this.info.store_Phone,
      })
    },

    //前往报价人列表
    quoteMember() {
      uni.navigateTo({
        url:
          '/pages_findAccessory/detailsInqParticipation/detailsInqParticipation?type=1&id=' +
          this.id,
      })
    },

    handleEffectiveTime(index) {
      const days = this.dateRage[index].value
      this.effectiveTime = new Date(
        new Date(new Date().toLocaleDateString()).getTime() + days * 86400000
      )
    },

    closPoupe() {
      this.successPoup = false
      uni.navigateBack()
    },
  },
}
